package com.yiren.backstage.currency.utils;

import com.yiren.backstage.config.bean.device.StationParamConfigBean;
import com.yiren.backstage.currency.bean.GpsInfoBean;
import com.yiren.backstage.currency.bean.LinePointBean;
import com.yiren.backstage.currency.bean.PointBean;

/**
 * @author 肖赞
 * @date :2022/3/10 15:09
 * @description: Gps工具
 */
public class GpsUtil {

    private final static double pi = 3.14159265358979323846264338327950288;
    private final static double x_pi = pi * 3000.0 / 180.0;
    private final static double a = 6378245.0;
    private final static double ee = 0.00669342162296594323;
    private final static double Rc = 6378137;
    private final static double Rj = 6356725;
    //纬度每度的距离（米）
    public final static float perLatitude = 110949.0f;
    //经度每度的距离基数（米），这里还要乘以cos（纬度）
    public final static float perLongitude = 111314.0f;

    /**
     * 求两经纬度点距离
     *
     * @param startGpsInfo
     * @param endGpsInfo
     * @return
     */
    public static double getDistance(GpsInfoBean startGpsInfo, GpsInfoBean endGpsInfo) {
        double radLat1 = (startGpsInfo.getLatitude() * Math.PI / 180.0);
        double radLat2 = (endGpsInfo.getLatitude() * Math.PI / 180.0);
        double a = radLat1 - radLat2;
        double b = (startGpsInfo.getLongitude() - endGpsInfo.getLongitude()) * Math.PI / 180.0;
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * Rc;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    /**
     * 求两经纬度点距离
     *
     * @param linePoint
     * @param endGpsInfo
     * @return
     */
    public static double getDistance(LinePointBean linePoint, GpsInfoBean endGpsInfo) {
        double radLat1 = (linePoint.getLatitude() * Math.PI / 180.0);
        double radLat2 = (endGpsInfo.getLatitude() * Math.PI / 180.0);
        double a = radLat1 - radLat2;
        double b = (linePoint.getLongitude() - endGpsInfo.getLongitude()) * Math.PI / 180.0;
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * Rc;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    /**
     * 已知点的经纬度 与另一个点的距离 以及方向角 求另一点的经纬度
     *
     * @param longitude
     * @param latitude
     * @param distance  单位KM
     * @param angle     方向角
     * @return
     */
    public static GpsInfoBean getMyLatLng(double longitude, double latitude, double distance, double angle) {
        double m_Latitude = latitude;
        double m_RadLo = longitude * Math.PI / 180.;
        double m_RadLa = latitude * Math.PI / 180.;
        double Ec = Rj + (Rc - Rj) * (90. - m_Latitude) / 90.;
        double Ed = Ec * Math.cos(m_RadLa);

        double dx = distance * 1000 * Math.sin(Math.toRadians(angle));
        double dy = distance * 1000 * Math.cos(Math.toRadians(angle));
        double bjd = (dx / Ed + m_RadLo) * 180. / Math.PI;
        double bwd = (dy / Ec + m_RadLa) * 180. / Math.PI;

        GpsInfoBean gps = new GpsInfoBean();
        gps.setLocation(bjd, bwd);
        return gps;
    }

    /**
     * 将gps84坐标转换为百度坐标
     *
     * @param lon
     * @param lat
     * @param high
     * @return
     */
    public static String getBdCoornates(double lat, double lon, double high) {
        GpsInfoBean gcj = gps84_To_Gcj02(lat, lon);
        GpsInfoBean bd = gcj02_To_Bd09(gcj.getLatitude(), gcj.getLongitude());
        return bd.getLongitude() + "," + bd.getLatitude() + "," + high;
    }

    /**
     * 84 to 火星坐标系 (GCJ-02) World Geodetic System ==> Mars Geodetic System
     *
     * @param lat
     * @param lon
     * @return
     */
    public static GpsInfoBean gps84_To_Gcj02(double lat, double lon) {
        if (outOfChina(lat, lon)) {
            return null;
        }
        double dLat = transformLat(lon - 105.0, lat - 35.0);
        double dLon = transformLon(lon - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * pi;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        double mgLat = lat + dLat;
        double mgLon = lon + dLon;
        GpsInfoBean g = new GpsInfoBean();
        g.setLocation(mgLon, mgLat);
        return g;
    }

    /**
     * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 将 GCJ-02 坐标转换成 BD-09 坐标
     *
     * @param gg_lat
     * @param gg_lon
     */
    public static GpsInfoBean gcj02_To_Bd09(double gg_lat, double gg_lon) {
        double x = gg_lon, y = gg_lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        double bd_lon = z * Math.cos(theta) + 0.0065;
        double bd_lat = z * Math.sin(theta) + 0.006;
        GpsInfoBean g = new GpsInfoBean();
        g.setLocation(bd_lon, bd_lat);
        return g;
    }

    private static boolean outOfChina(double lat, double lon) {
        if (lon < 72.004 || lon > 137.8347)
            return true;
        if (lat < 0.8293 || lat > 55.8271)
            return true;
        return false;
    }

    private static double transformLat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    private static double transformLon(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
        return ret;
    }

    /**
     * 根据两个点的经度坐标获取东坐标
     *
     * @param lon1 基站经度
     * @param lat1 基站纬度
     * @param lon2 采集的经度
     */
    public static float getDistanceX(double lon1, double lat1, double lon2) {
        return (float) (perLongitude * (lon2 - lon1) * Math.cos(Math.toRadians(lat1)));
    }

    /**
     * 根据两个点的纬度坐标获取北坐标
     *
     * @param lat1 基站纬度
     * @param lat2 采集的纬度
     */
    public static float getDistanceY(double lat1, double lat2) {
        return (float) (perLatitude * (lat2 - lat1));
    }

    /**
     * 根据东坐标获取经度
     *
     * @param lon       基站经度
     * @param lat       基站纬度
     * @param distanceX 东坐标
     * @return
     */
    public static double getCurLongitude(double lon, double lat, float distanceX) {
        return lon + distanceX / (perLongitude / Math.cos(Math.toRadians(lat)));
    }

    /**
     * 根据北坐标获取纬度
     *
     * @param lat       基站纬度
     * @param distanceY 北坐标
     * @return
     */
    public static double getCurLatitude(double lat, float distanceY) {
        return lat + distanceY / perLatitude;
    }

    /**
     * 根据两个点的高度坐标获取天坐标
     *
     * @param high1 基站高度
     * @param high2 采集点高度
     * @return
     */
    public static float getDistanceZ(double high1, double high2) {
        return (float) (high2 - high1);
    }

    /**
     * 获取指定距离内的经度的差值
     *
     * @param distance 指定距离
     * @param lat      所在纬度
     * @return
     */
    public static double getDistanceLon(double distance, double lat) {
        return distance / (perLongitude * Math.cos(Math.toRadians(lat)));
    }

    /**
     * 获取指定距离内的纬度的差值
     *
     * @param distance 指定距离
     * @return
     */
    public static double getDistanceLat(double distance) {
        return distance / perLatitude;
    }

    public static boolean bearingMatch(GpsInfoBean gps1, GpsInfoBean gps2, double trackError) {
        // 计算两个方位最小夹角
        double minTrack = Math.abs(gps1.getBearing() - gps2.getBearing());
        // 方位匹配
        return (minTrack < 360 - minTrack ? minTrack : 360 - minTrack) < trackError;
    }

    public static boolean bearingMatch(GpsInfoBean gps1, LinePointBean linePoint, double trackError) {
        // 计算两个方位最小夹角
        double minTrack = Math.abs(gps1.getBearing() - linePoint.getBearing());
        // 方位匹配
        return (minTrack < 360 - minTrack ? minTrack : 360 - minTrack) < trackError;
    }

    public static double convertToDecimal(String du) {
        double tmp = Double.valueOf(du) * 10000;
        double tmpDu = Math.floor(tmp / 1000000);
        tmp = tmp % 1000000;
        tmpDu += tmp / 600000;
        return tmpDu;
    }

    /**
     * 获取当前GPS与点位之间的距离
     * @return
     */
    public static double getDistanceGpsPoint(GpsInfoBean gpsInfo, PointBean point,StationParamConfigBean stationParamConfigBean) {
        String[] baseLocations = stationParamConfigBean.getBaseStationLocation().split(",");
        double longitude = Double.parseDouble(baseLocations[0]);
        double latitude = Double.parseDouble(baseLocations[1]);
        double distance = Math.sqrt(Math.pow((GpsUtil.getDistanceX(longitude, latitude, gpsInfo.getLongitude()) - point.getX()), 2) + Math.pow((GpsUtil.getDistanceY(latitude, gpsInfo.getLatitude()) - point.getY()), 2));
        return distance;
    }

    /**
     * 将gps84坐标转换为百度坐标
     *
     * @param lon
     * @param lat
     * @param high
     * @return
     */
    public String getBdCoordinates(double lat, double lon, double high) {
        GpsInfoBean gcj = gps84_To_Gcj02(lat, lon);
        if (null != gcj) {
            GpsInfoBean bd = gcj02_To_Bd09(gcj.getLatitude(), gcj.getLongitude());
            return bd.getLongitude() + "," + bd.getLatitude() + "," + high;
        }
        return "";
    }
}
